package com.grapes.view;

import java.lang.reflect.Method;

import com.grapes.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;

public class GrapesActivity extends Activity {
	protected static final float MIN_HORIZONTAL_FlING_DISTANCE = 10.0f;
	protected static final float MIN_HORIZONTAL_FLIGN_VALOCITY = 50.0f;
	private final int PROGRESS_DIALOG = 1;
	private final int ALERT_DIALOG = 2;
	private final int CONFIRMATION_DIALOG = 3;
	private int megId;
	private int positiveButtonMsg;
	private int negativeBtnMsg;
	private String negativeMethodName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
	}

	protected void goBack()
	{
		finish();
		overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
	}
	
	@Override
	public void onBackPressed() {
		// do nothing,just make the back key not functions.
		goBack();
	}

	public void showProgressDialog() {
		showDialog(PROGRESS_DIALOG);
	}

	public void dismissPorgressDialog() {
		removeDialog(PROGRESS_DIALOG);
	}

	public void showAlertDialog(int msgId) {
		this.megId = msgId;
		showDialog(ALERT_DIALOG);
	}

	public void showConfirmationDialog(int msgId, int positiveButtonMsg,
			int negativeBtnMsg, String negativeMethodName) {
		this.megId = msgId;
		this.positiveButtonMsg = positiveButtonMsg;
		this.negativeBtnMsg = negativeBtnMsg;
		this.negativeMethodName = negativeMethodName;
		showDialog(CONFIRMATION_DIALOG);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case ALERT_DIALOG:
			return createAlertDialog();
		case PROGRESS_DIALOG:
			return ProgressDialog.show(this, "", "Get Brews. Please wait...",
					true);
		case CONFIRMATION_DIALOG:
			return createConfirmDialog();
		}
		return super.onCreateDialog(id);
	}

	private Dialog createAlertDialog() {
		return new AlertDialog.Builder(this)
				.setTitle("Error")
				.setMessage(megId)
				.setPositiveButton(R.string.ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								removeDialog(ALERT_DIALOG);
							}
						}).create();
	}

	private Dialog createConfirmDialog() {
		return new AlertDialog.Builder(this)
				.setTitle("Confirmation")
				.setMessage(getResources().getString(megId))
				.setNegativeButton(getResources().getString(negativeBtnMsg),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								try {
									Method execMethod = GrapesActivity.this
											.getClass().getMethod(
													negativeMethodName);
									execMethod.invoke(GrapesActivity.this);
									removeDialog(CONFIRMATION_DIALOG);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						})
				.setPositiveButton(getResources().getString(positiveButtonMsg),
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								removeDialog(CONFIRMATION_DIALOG);
							}
						}).create();
	}
}
